//
//  ThreeDDirectView.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ThreeDDirectView.h"
#import <QuartzCore/QuartzCore.h>

@implementation ThreeDDirectView

@synthesize currentType=_currentType;
@synthesize titleLabel=_titleLabel;

-(void)dealloc{

    [contentView release];
    [selectViewArray release];
    [bellSelectViewArray release];
    [_titleLabel release];
    [pageControl release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f].CGColor;
        self.layer.cornerRadius=6;
        
        float tempWidth=frame.size.width-20.0f;
        
        ThreeDSelectView *firstSelectView=[[ThreeDSelectView alloc]initWithFrame:CGRectMake(10.0f,15.0f, tempWidth, 40.0f)];
        firstSelectView.tag=0;
        [firstSelectView addTarget:self action:@selector(selectViewIsTouch:) forControlEvents:UIControlEventTouchUpInside];
        firstSelectView.placeholder=@"点击这里选择百位数";
        firstSelectView.bitTitle=@"百";
        [self addSubview:firstSelectView];
        
        firstSelectView.isSelected=YES;
        
        ThreeDSelectView *secoundSelectView=[[ThreeDSelectView alloc]initWithFrame:CGRectMake(10.0f, 15.0f+40.0f+10.0f, tempWidth, 40.0f)];
        [secoundSelectView addTarget:self action:@selector(selectViewIsTouch:) forControlEvents:UIControlEventTouchUpInside];
        secoundSelectView.tag=1;
        secoundSelectView.placeholder=@"点击这里选择十位数";
        secoundSelectView.bitTitle=@"十";
        [self addSubview:secoundSelectView];
        
        ThreeDSelectView *thirdSelectView=[[ThreeDSelectView alloc]initWithFrame:CGRectMake(10.0f, 15.0f+40.0f+10.0f+40.0f+10.0f, tempWidth, 40.0f)];
        [thirdSelectView addTarget:self action:@selector(selectViewIsTouch:) forControlEvents:UIControlEventTouchUpInside];
        thirdSelectView.tag=2;
        thirdSelectView.placeholder=@"点击这里选择个位数";
        thirdSelectView.bitTitle=@"个";
        [self addSubview:thirdSelectView];
        
        
        selectViewArray=[[NSArray alloc]initWithObjects:firstSelectView,secoundSelectView,thirdSelectView, nil];
        [firstSelectView release];
        [secoundSelectView release];
        [thirdSelectView release];
        
        _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10.0f, thirdSelectView.frame.origin.y+40.0f+10.0f, tempWidth, 15.0f)];
        _titleLabel.backgroundColor=[UIColor clearColor];
        _titleLabel.textColor=[UIColor blackColor];
        _titleLabel.font=[UIFont systemFontOfSize:14.0f];
        [self addSubview:_titleLabel];
        _titleLabel.textAlignment=UITextAlignmentCenter;
        _titleLabel.text=@"每位至少选1个号,单注最高奖金1000元";
        
        contentView=[[UIScrollView alloc]initWithFrame:CGRectMake(10.0f, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+10.0f, tempWidth, 80)];
        contentView.pagingEnabled=YES;
        contentView.contentSize=CGSizeMake(contentView.frame.size.width*3, contentView.frame.size.height);
        contentView.delegate=self;
        contentView.showsHorizontalScrollIndicator=NO;
        contentView.showsVerticalScrollIndicator=NO;
        [self addSubview: contentView];
        
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(10.0f, contentView.frame.origin.y+contentView.frame.size.height, contentView.frame.size.width, 44.0f)];
        pageControl.backgroundColor=[UIColor clearColor];
        pageControl.numberOfPages=3;
        [self addSubview:pageControl];
        [pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchUpInside];
        
        NSMutableArray *tempArray=[NSMutableArray new];
        for (int i=0; i<selectViewArray.count; i++) {
            BellSelectView *view=[[BellSelectView alloc]initWithFrame:CGRectMake(i*contentView.frame.size.width, 0.0f, contentView.frame.size.width, contentView.frame.size.height)];
            ThreeDSelectView *selectView=[selectViewArray objectAtIndex:i];
            selectView.selectView=view;
            view.selectView=selectView;
            [tempArray addObject:view];
            [contentView addSubview:view];
        }
        bellSelectViewArray=[[NSArray alloc]initWithArray:tempArray];
        [tempArray release];
        
    }
    return self;
}


-(void)setCurrentType:(NSInteger)currentType{

    if (currentType==_currentType) {
        return;
    }
    ThreeDSelectView *view=[selectViewArray objectAtIndex:_currentType];
    view.isSelected=NO;
    ThreeDSelectView *selectView=[selectViewArray objectAtIndex:currentType];
    selectView.isSelected=YES;
    _currentType=currentType;
    [contentView scrollRectToVisible:CGRectMake(currentType*contentView.frame.size.width, 0.0f, contentView.frame.size.width, contentView.frame.size.height) animated:YES];
    pageControl.currentPage=currentType;
}

-(void)pageChange:(UIPageControl *)page{

    self.currentType=page.currentPage;
}

-(void)selectViewIsTouch:(UIView *)view{

    self.currentType=view.tag;
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSInteger num=scrollView.contentOffset.x/scrollView.frame.size.width;
    self.currentType=num;
}

- (void)randomSelect3Direct
{
    
    
    int i=0;
    while (i<selectViewArray.count) {
        int index = arc4random() % 9;
        ThreeDSelectView *DSelect=[selectViewArray objectAtIndex:i];
        
        [DSelect removeAllNumber];
        
        [DSelect addNumber:index];
    
        [DSelect.selectView removeAllnum];
        [DSelect.selectView randomNumber:index];
        
        i++;
    }

    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

@interface ThreeDSelectView (Private)

-(void)setDefault;

@end

@implementation ThreeDSelectView

@synthesize selectNumArray=_selectNumArray;
@synthesize isSelected=_isSelected;
@synthesize bitTitle=_bitTitle;
@synthesize placeholder=_placeholder;
@synthesize selectView=_selectView;

-(void)dealloc{

    [bitBackImageView release];
    [placeLabel release];
    [bitLabel release];
    [selectBellContentView release];
    [selectBellArray release];
    [_selectNumArray release];
    [super dealloc];
}

-(void)setDefault{
    
    self.layer.borderWidth=1.0f;
    self.layer.borderColor=[UIColor redColor].CGColor;
    self.layer.cornerRadius=6.0f;

    bitBackImageView=[[UIImageView alloc]initWithFrame:CGRectMake(8.0f, 8.0f, 24.0f, 24.0f)];
    bitBackImageView.image=[UIImage imageNamed:@"bit_normal.png"];
    [self addSubview:bitBackImageView];
    
    bitLabel=[[UILabel alloc]initWithFrame:CGRectMake(0.0f, 0.0f, bitBackImageView.frame.size.width, bitBackImageView.frame.size.height)];
    bitLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    bitLabel.backgroundColor=[UIColor clearColor];
    bitLabel.textAlignment=UITextAlignmentCenter;
    [bitBackImageView addSubview:bitLabel];
    bitLabel.shadowColor=[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.6f];
    bitLabel.textColor=[UIColor whiteColor];
    bitLabel.font=[UIFont systemFontOfSize:15.0f];
    
    selectBellContentView=[[UIScrollView alloc]initWithFrame:CGRectMake(40.0f, 0.0f,self.frame.size.width-40.0f-10.0f, 40.0f)];
    selectBellContentView.backgroundColor=[UIColor clearColor];
    [self addSubview:selectBellContentView];
    
    placeLabel=[[UILabel alloc]initWithFrame:CGRectMake(10.0f, 0.0f, 0.0f, 0.0f)];
    placeLabel.backgroundColor=[UIColor clearColor];
    placeLabel.userInteractionEnabled=YES;
    placeLabel.textColor=[UIColor grayColor];
   // placeLabel.shadowColor=[UIColor whiteColor];
    placeLabel.font=[UIFont systemFontOfSize:14.0f];
  //  placeLabel.shadowOffset=CGSizeMake(0.0f, 1.0f);
    placeLabel.text=@"点击这里选择百位数";
    [placeLabel sizeToFit];
    placeLabel.frame=CGRectMake(10.0f, 40.0f/2-placeLabel.frame.size.height/2, placeLabel.frame.size.width, placeLabel.frame.size.height);
    [selectBellContentView addSubview:placeLabel];
    
    UIButton *tempBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    tempBtn.frame=CGRectMake(0.0f, 0.0f, placeLabel.frame.size.width, placeLabel.frame.size.height);
    [tempBtn addTarget:self action:@selector(sendAction) forControlEvents:UIControlEventTouchUpInside];
    [placeLabel addSubview:tempBtn];
                             
    
    _selectNumArray=[NSMutableArray new];
    selectBellArray=[NSMutableArray new];
    
}

-(void)sendAction{

    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

-(id)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];
    if (self) {
        
        [self setDefault];
    }
    
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{

    self=[super initWithCoder:aDecoder];
    if (self) {
        
        [self setDefault];
    }
    
    return self;
}

-(void)setIsSelected:(BOOL)isSelected{

    if (self.isSelected==isSelected) {
        return;
    }
    
    _isSelected=isSelected;
    
    UIImage *bitImage=nil;
    
    if (isSelected) {
        bitImage=[UIImage imageNamed:@"bit_highted.png"];
        bitLabel.textColor=[UIColor blackColor];
        bitLabel.shadowColor=[UIColor whiteColor];
        bitLabel.shadowOffset=CGSizeMake(0.0f, 1.0f);
    }else {
        bitImage=[UIImage imageNamed:@"bit_normal.png"];
        bitLabel.textColor=[UIColor whiteColor];
        bitLabel.shadowOffset=CGSizeMake(0.0f,-1.0f);
        bitLabel.shadowColor=[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.6f];
    }
    
    bitBackImageView.image=bitImage;
}

-(void)setBitTitle:(NSString *)bitTitle{

    bitLabel.text=bitTitle;
}

-(NSString *)bitTitle{

    return bitLabel.text;
}

-(void)setPlaceholder:(NSString *)placeholder{

    placeLabel.text=placeholder;
    [placeLabel sizeToFit];
}

-(NSString *)placeholder{

    return placeLabel.text;
}

-(void)bellDone:(BellButton *)btn{

    [self removeNumber:btn.tag];
}

-(BOOL)addNumber:(NSInteger)number{

    NSNumber *theNumber=[NSNumber numberWithInt:number];
    if ([_selectNumArray containsObject:theNumber]) {
        return NO;
    }
    
    if (_selectNumArray.count==0) {
        [placeLabel removeFromSuperview];
    }
  
    BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(10.0f+_selectNumArray.count*(30.0f+10.0f), 5.0f, 30.0f, 30.0f)];
    [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
    btn.bellColor=BellColorRed;
    btn.bellState=BellStateSelct;
    [selectBellArray addObject:btn];
    [selectBellContentView addSubview:btn];
    [selectBellContentView setContentSize:CGSizeMake(btn.frame.origin.x+btn.frame.size.width+10.0f, selectBellContentView.frame.size.height)];
    [_selectNumArray addObject:theNumber];
    [self reloadBellNumber];
    return YES;
}

-(BOOL)removeNumber:(NSInteger)number{

    NSNumber *theNumber=[NSNumber numberWithInt:number];
    if (![_selectNumArray containsObject:theNumber]) {
        return NO;
    }
    [[selectBellArray lastObject]removeFromSuperview];
    [selectBellArray removeLastObject];
    [_selectNumArray removeObject:theNumber];
    [self reloadBellNumber];
    
    [_selectView removeNumber:number];
    if (_selectNumArray.count==0) {
        [selectBellContentView addSubview:placeLabel];
    }
    return YES;
}

-(void)removeAllNumber{

    for (UIView *view in selectBellArray) {
        [view removeFromSuperview];
    }
    [selectBellArray removeAllObjects];
    [_selectNumArray removeAllObjects];
    [placeLabel removeFromSuperview];
}

-(void)reloadBellNumber{

    [_selectNumArray sortUsingSelector:@selector(compare:)];
    
    [selectBellContentView scrollRectToVisible:CGRectMake(selectBellContentView.contentSize.width-selectBellContentView.frame.size.width, 0.0f, selectBellContentView.frame.size.width, selectBellContentView.frame.size.height) animated:YES];
    
    for ( int i=0; i<_selectNumArray.count; i++) {
        NSInteger number=[[_selectNumArray objectAtIndex:i] intValue];
        BellButton *btn=[selectBellArray objectAtIndex:i];
        btn.number=number;
        btn.tag=number;
    }
}

@end


@implementation BellSelectView

@synthesize selectView=_selectView;

-(id)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
        float margin=(frame.size.width-40.0f-5*30.0f)/4;
        
        for (int i=0; i<10; i++) {
            NSInteger lines=i/5;
            NSInteger count=i-lines*5;
            
            BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(15.0f+count*(30.0f+margin), lines*(30.0f+20.0f), 30.0f, 30.0f)];
            btn.number=i;
            btn.bellColor=BellColorRed;
            btn.tag=i;
            btn.bellState=BellStateNormal;
            [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview: btn];
        }
    }
    return self;
}


-(void)bellDone:(BellButton *)btn{

    if (btn.bellState==BellStateNormal) {

        if (_selectView) {
            
            [_selectView addNumber:btn.tag];
            
            btn.bellState=BellStateSelct;
        }
    }else {
        if (_selectView) {
            [_selectView removeNumber:btn.tag];
            btn.bellState=BellStateNormal;
        }
    }
}

-(void)removeNumber:(NSInteger)number{

    for (BellButton *btn in self.subviews) {
        if (btn.tag==number) {
            btn.bellState=BellStateNormal;
        }
    }
}

- (void)removeAllnum{
    
    for (BellButton *btn in self.subviews) {
  
            btn.bellState=BellStateNormal;
       
    }
    
}

-(void)randomNumber:(NSInteger)number{
    
    for (BellButton *btn in self.subviews) {
        if (btn.tag==number) {
            btn.bellState=BellStateSelct;
        }
    }
    
}

@end